package edu.ec.java02.group02.service.impl;

import edu.ec.java02.group02.domain.*;
import edu.ec.java02.group02.mapper.*;
import edu.ec.java02.group02.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private CartMapper cartMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private ProductInfoMapper productInfoMapper;

    @Override
    public UserInfo login(UserInfo userInfo) {
        UserInfo user = userInfoMapper.getUserInfo(userInfo);
        if(user != null)
            return user;
        return  null;
    }


    /**
     * 注册用户
     * @param userInfo
     * @return
     */
    @Override
    public boolean register(UserInfo userInfo) {
        int i=userInfoMapper.insert(userInfo);
        if(i==1){
            return true;
        }
        return false;
    }


    /**
     * 加入购物车
     * @param cart
     * @return
     */
    @Override
    public boolean saveCartInfo(Cart cart) {
        int i=cartMapper.insert(cart);
        if(i==1){
            return  true;
        }
        return false;
    }

    /**
     * 购物车结算
     * @param cart
     * @return
     */
    @Override
    public boolean buyProductFromCart(Cart cart) {
        Order order=new Order();
        OrderDetail orderDetail=new OrderDetail();

        UUID uuid= UUID.randomUUID();
        Date date=new Date(System.currentTimeMillis());

        order.setOrderStatus("未支付");
        order.setOrderNo(uuid.toString().substring(0, 8));
        order.setOrderCreatTime(date);

        ProductInfo pro=productInfoMapper.selectByPrimaryKey(cart.getProductId());

        orderDetail.setOrderNo(uuid.toString().substring(0, 8));
        orderDetail.setProductId(cart.getProductId());
        orderDetail.setProductName(pro.getProductName());
        orderDetail.setUserId(cart.getUserId());
        orderDetail.setProductNumber(cart.getProductNumber());
        orderDetail.setProductTotalprice(pro.getProductPrice()*cart.getProductNumber());
        if(saveOrderInfo(order,orderDetail)){
            return  true;
        }
        return false;
    }

    /**
     * 修改个人信息
     * @param userInfo
     * @return
     */
    @Override
    public boolean modifyUserInfo(UserInfo userInfo) {
        int i=userInfoMapper.updateByPrimaryKey(userInfo);
        if(i==1){
            return true;
        }
        return false;
    }

    /**
     * 下订单结算
     * @param order
     * @param orderDetail
     * @return
     */
    @Override
    public boolean saveOrderInfo(Order order, OrderDetail orderDetail) {
        int pronum = productInfoMapper.selectByPrimaryKey(orderDetail.getProductId()).getProductInventory();
        if((pronum-orderDetail.getProductNumber())>0){
            ProductInfo productInfo = new ProductInfo();
            productInfo.setProductId(orderDetail.getProductId());
            productInfo.setProductInventory(pronum-orderDetail.getProductNumber());
            productInfo.setProductName(orderDetail.getProductName());
            int pi=productInfoMapper.updateByPrimaryKeySelective(productInfo);
            if(pi>0){
                int i=orderMapper.insert(order);
                if(i==1){
                    int j=orderDetailMapper.insert(orderDetail);
                    if(j==1){
                        return true;
                    }
                }
            }

        }
        return false;
    }

    /**
     * 取消订单
     * @param orderNo
     * @return
     */
    @Override
    public boolean removeOrderInfo(int orderNo) {
        Order order=orderMapper.selectByPrimaryKey(orderNo);
        order.setOrderStatus("已取消");
        //order.setOrderCancelTime();
        int i=orderMapper.updateByPrimaryKey(order);
        if(i==1){
            return  true;
        }
        return false;
    }

    /**
     * 查看订单
     * @param id
     * @return
     */
    @Override
    public List<OrderDetail> findOrderDetailInfo(int id) {
        List<OrderDetail> ls=orderDetailMapper.selectListOrderdetail(id);
        if(ls!=null){
            return ls;
        }
        return null;
    }
    @Override
    public Order findOrderInfo(String  str) {
        Order order=orderMapper.selectOrderOne(str);
        if(order!=null){
            return order;
        }
        return null;
    }

    @Override
    public List<ProductInfo> listProductInfo(String str) {
        List<ProductInfo> list=productInfoMapper.selectListPro(str);
        return list;
    }

    @Override
    public Order findOrderInfo(int orderNo) {
        return null;
    }

    @Override
    public ProductInfo selectProductInfo(Integer id) {
        ProductInfo productInfo=productInfoMapper.selectByPrimaryKey(id);
        return productInfo;
    }
    @Override
    public List<ProductInfo> selectProductType(String str){
        List<ProductInfo> ls=productInfoMapper.selectListProType(str);
        return ls;
    }
    public Order selectOrderKey(int id){
        Order order = orderMapper.selectByPrimaryKey(id);
        return order;
    }

    @Override
    public void changeUserInfo(UserInfo userInfo, String userName, String userPhone) {
        userInfo.setUserName(userName);
        userInfo.setUserPhone(userPhone);
        userInfoMapper.updateUser(userInfo);
    }

    public  boolean updateOrder(Order order){
        int i= orderMapper.updateByPrimaryKeySelective(order);
        if(i>0){
            return  true;
        }
        return false;
    }
}
